package com.ruoyi.subjectproctoring.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.subjectproctoring.domain.Proctoringassignment;
import com.ruoyi.subjectproctoring.mapper.SubjectproctoringMapper;
import com.ruoyi.subjectproctoring.domain.Subjectproctoring;
import com.ruoyi.subjectproctoring.service.ISubjectproctoringService;

/**
 * 监考需求Service业务层处理
 * 
 * @author tnx
 * @date 2024-11-24
 */
@Service
public class SubjectproctoringServiceImpl implements ISubjectproctoringService 
{
    @Autowired
    private SubjectproctoringMapper subjectproctoringMapper;

    /**
     * 查询监考需求
     * 
     * @param id 监考需求主键
     * @return 监考需求
     */
    @Override
    public Subjectproctoring selectSubjectproctoringById(Long id)
    {
        return subjectproctoringMapper.selectSubjectproctoringById(id);
    }

    /**
     * 查询监考需求列表
     * 
     * @param subjectproctoring 监考需求
     * @return 监考需求
     */
    @Override
    public List<Subjectproctoring> selectSubjectproctoringList(Subjectproctoring subjectproctoring)
    {
        return subjectproctoringMapper.selectSubjectproctoringList(subjectproctoring);
    }

    /**
     * 新增监考需求
     * 
     * @param subjectproctoring 监考需求
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSubjectproctoring(Subjectproctoring subjectproctoring)
    {
        int rows = subjectproctoringMapper.insertSubjectproctoring(subjectproctoring);
        insertProctoringassignment(subjectproctoring);
        return rows;
    }

    /**
     * 修改监考需求
     * 
     * @param subjectproctoring 监考需求
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSubjectproctoring(Subjectproctoring subjectproctoring)
    {
        subjectproctoringMapper.deleteProctoringassignmentBySubjectId(subjectproctoring.getId());
        insertProctoringassignment(subjectproctoring);
        return subjectproctoringMapper.updateSubjectproctoring(subjectproctoring);
    }

    /**
     * 批量删除监考需求
     * 
     * @param ids 需要删除的监考需求主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSubjectproctoringByIds(Long[] ids)
    {
        subjectproctoringMapper.deleteProctoringassignmentBySubjectIds(ids);
        return subjectproctoringMapper.deleteSubjectproctoringByIds(ids);
    }

    /**
     * 删除监考需求信息
     * 
     * @param id 监考需求主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSubjectproctoringById(Long id)
    {
        subjectproctoringMapper.deleteProctoringassignmentBySubjectId(id);
        return subjectproctoringMapper.deleteSubjectproctoringById(id);
    }

    /**
     * 新增分配记录信息
     * 
     * @param subjectproctoring 监考需求对象
     */
    public void insertProctoringassignment(Subjectproctoring subjectproctoring)
    {
        List<Proctoringassignment> proctoringassignmentList = subjectproctoring.getProctoringassignmentList();
        Long id = subjectproctoring.getId();
        if (StringUtils.isNotNull(proctoringassignmentList))
        {
            List<Proctoringassignment> list = new ArrayList<Proctoringassignment>();
            for (Proctoringassignment proctoringassignment : proctoringassignmentList)
            {
                proctoringassignment.setSubjectId(id);
                list.add(proctoringassignment);
            }
            if (list.size() > 0)
            {
                subjectproctoringMapper.batchProctoringassignment(list);
            }
        }
    }
}
